﻿using System;
using System.Collections.Generic;
using System.Linq;
using Web.Models;
using Model.Core.DataBase;
using Web.Models.AnalysisData;
using Web.Models.CoreData;


namespace Web.BML.CoreData
{
    /// <summary>
    /// 公司用户
    /// </summary>
    public class CompanyOp
    {

        private static CompanyOp instance;

        /// <summary>
        /// 构造函数
        /// </summary>
        private CompanyOp()
        {

        }
        /// <summary>
        /// 单例模式实例化
        /// </summary>
        /// <returns></returns>
        public static CompanyOp GetInstance()
        {
            if (instance == null)
            {
                instance = new CompanyOp();
            }
            return instance;
        }

        

        /// <summary>
        ///    查询用户信息
        /// </summary>
        /// <param name="id">id</param>
        /// <returns></returns>
        public Company QueryCompany(String id)
        {
            using (IDbContext db = ContainerFactory.OpenCoreConnect())
            {
                return db.Queryable<Company>().FirstOrDefault(m => m.Id == id);
            }
        }

        /// <summary>
        ///  查询用户信息-通过名称
        /// </summary>
        /// <param name="name">名称</param>
        /// <returns></returns>
        public Company QueryCompanyByName(String name)
        {
            using (IDbContext db = ContainerFactory.OpenCoreConnect())
            {
                return db.Queryable<Company>().FirstOrDefault(m => m.Name.Equals(name));
            }
        }

        /// <summary>
        /// 查询所有用户信息
        /// </summary>
        /// <returns></returns>
        public List<Company> QueryAllCompanyList()
        {
            using (IDbContext db = ContainerFactory.OpenCoreConnect())
            {
                return db.Queryable<Company>().ToList();
            }
        }
        
        /// <summary>
        /// 修改用户信息
        /// </summary>
        /// <param name="model"></param>
        public void ModifCompany(Company model)
        {
            using (IDbContext db = ContainerFactory.OpenCoreConnect())
            {
                db.GetOperator<Company>().Modify(model);
                db.SaveAllChange();
            }
        }

        /// <summary>
        /// 创建用户信息
        /// </summary>
        /// <param name="model"></param>
        public void CreateCompany(Company model)
        {
            using (IDbContext db = ContainerFactory.OpenCoreConnect())
            {
                db.GetOperator<Company>().Create(model);
                db.SaveAllChange();
            }
        }

        /// <summary>
        /// 删除用户信息
        /// </summary>
        /// <param name="model"></param>
        public void DeleteCompany(Company model)
        {
            using (IDbContext db = ContainerFactory.OpenCoreConnect())
            {
                db.GetOperator<Company>().Destroy(model);
                db.SaveAllChange();
            }
        }
    }


}

